package ch.sv7.tool.dbmanager.db.xml;

import java.io.InputStream;
import java.sql.Types;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;
import ch.sv7.tool.dbmanager.db.ColumnSchema;
import ch.sv7.tool.dbmanager.db.ForeignKeySchema;
import ch.sv7.tool.dbmanager.db.IndexSchema;
import ch.sv7.tool.dbmanager.db.TableSchema;

/**
 *
 * @author svt
 *
 */
public class XmlDatabaseSchemaFactoryTestCase extends TestCase {

	public void testSchemaReading() throws Exception  {
		
		InputStream stream = this.getClass().getResourceAsStream("schema_1.xml");
		assertNotNull("Schema file not found", stream);
		XmlDatabaseSchemaFactory factory = new XmlDatabaseSchemaFactory(stream, null);
		
		List tables = Arrays.asList(factory.getTables());
		assertEquals(1, tables.size());
		tables.contains("MYTABLE");

		TableSchema ts = factory.getTableSchema("MYTABLE");
	
		assertNotNull(ts);
		assertEquals("MYTABLE", ts.getName());
		/*
		 * Basic checks
		 */
		ColumnSchema cs0 = ts.getColumnSchema("ID");
		assertNotNull(cs0);
		assertEquals(Types.INTEGER, cs0.getType().getJDBCType());
		assertEquals(0, cs0.getType().getSize());		
		assertEquals(true, cs0.isPrimaryKey());		

		ColumnSchema cs1 = ts.getColumnSchema("NAME");
		assertNotNull(cs1);
		assertEquals(Types.VARCHAR, cs1.getType().getJDBCType());
		assertEquals(20, cs1.getType().getSize());
		assertEquals(true, cs1.getType().isNullable());
		assertEquals(null, cs1.getType().getDefaultValue());
		assertEquals(false, cs1.isPrimaryKey());		

		ColumnSchema cs2 = ts.getColumnSchema("NUMBER");
		assertNotNull(cs2);
		assertEquals(Types.INTEGER, cs2.getType().getJDBCType());
		assertEquals(0, cs2.getType().getSize());
		assertEquals(false, cs2.isPrimaryKey());		

		ColumnSchema cs3 = ts.getColumnSchema("NAME_NOT_NULL");
		assertNotNull(cs3);
		assertEquals(Types.VARCHAR, cs3.getType().getJDBCType());
		assertEquals(20, cs3.getType().getSize());
		assertEquals(false, cs3.getType().isNullable());
		assertEquals(null, cs3.getType().getDefaultValue());
		assertEquals(false, cs3.isPrimaryKey());		
		
		ColumnSchema cs4 = ts.getColumnSchema("NAME_DEF");
		assertNotNull(cs4);
		assertEquals(Types.VARCHAR, cs4.getType().getJDBCType());
		assertEquals(20, cs4.getType().getSize());
		assertEquals(false, cs4.getType().isNullable());
		assertEquals("'EMPTY'", cs4.getType().getDefaultValue());
		assertEquals(false, cs4.isPrimaryKey());		

		/*
		 * Index checks
		 */
		List indexes = ts.getIndexes();
		assertNotNull(indexes);
		assertEquals(2, indexes.size()); // PK + MY_INDEX
		
		IndexSchema index = (IndexSchema)indexes.get(0);
		assertNotNull(index);
		assertEquals("ID", index.getColumns());
		
		index = (IndexSchema)indexes.get(1);
		assertNotNull(index);
		assertEquals("MY_INDEX", index.getName());
		assertEquals("NAME, NUMBER", index.getColumns());
		
		factory = new XmlDatabaseSchemaFactory(
				this.getClass().getResourceAsStream("schema_2.xml"), null);
		
		
		ts = factory.getTableSchema("MYTABLE");
		assertEquals(1, ts.getForeignKeys().size());
		TableSchema tsRef = factory.getTableSchema("MYTABLEREF");
		assertEquals(0, tsRef.getForeignKeys().size());
		ForeignKeySchema fk = (ForeignKeySchema)ts.getForeignKeys().get(0);
		assertNotNull(fk);
		assertEquals("MY_REF", fk.getName());
		assertEquals("MYTABLEREF", fk.getForeignTable());
		assertEquals("REF", fk.getForeignColumns());

		
	}
}
